package com.dc.cameraxtest.utils

import android.graphics.Bitmap
import android.util.Log
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions

/**
 * @projectName uv
 * @description:
 * @author dc
 * @date :2025.2.24 14:59
 */
class RecognizeTextUtils {
    companion object {
        private const val TAG = "RecognizeTextUtils"

        fun recognizeTextFromBitmap(
            bitmap: Bitmap,
            callback: (String) -> Unit  // 通过回调返回结果
        ) {
            val image = InputImage.fromBitmap(bitmap, 0)
            val options = ChineseTextRecognizerOptions.Builder().build()
            val recognizer = TextRecognition.getClient(options)

            recognizer.process(image)
                .addOnSuccessListener { text ->
                    val resultText = text.text
                    Log.e(TAG, "onSuccess: $resultText")
                    callback(resultText)  // 通过回调返回结果
                    recognizer.close()
                }
                .addOnFailureListener { e ->
                    e.printStackTrace()
                    callback("")  // 返回空或错误标识
                    recognizer.close()
                }
        }

    }


}